/*
 * DG (DG Gaming Engine)  - Server
 * Copyright (C) 2004  Bob Marks (marksie531@yahoo.com)
 * http://code.google.com/p/dng-gaming
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.dg.gaming.server;

import java.util.*;

import com.dg.gaming.api.common.*;

/**
 * List of Connection objects.
 *
 * @author  Bob Marks
 * @version Beta 0.3
 */
public class ConnectionList {

    /** Hashmap of connections. */
    private HashMap connections;

    /**
     * Constructor which sets up a Hash to store the various Connection
     * objects in.
     */
    public ConnectionList() {
        this.connections = new HashMap();
    }

    /**
     * Create a new Connection object using a ServerConnectionThread and add
     * to the connections HashMap using the username as the key.
     *
     * @param gameId 	       Game id e.g. "chess"
     * @param username         Username of person.
     * @param connectionThread ServerConnectionThread.
     */
    public void addConnection(String gameId, long username,
                              ServerConnectionThread connectionThread) {
        Connection conn = new Connection(connectionThread);

        connections.put(getKey(gameId, username), conn);
    }

    /**
     * Set the administrator thread.
     *
     * @param gameId 	       Game id e.g. "chess"
     * @param username         Username of person.
     * @param connectionThread ServerConnectionThread.
     */
    public void setAdminConnection(ServerConnectionThread connectionThread) {
        addConnection(IGame.ADMINISTRATOR_GAME, IGame.ADMINISTRATOR,
                      connectionThread);
    }

    /**
     * Return a serverconnection thread for a specified user (which is a field
     * of the Connection object).
     *
     * @param username Username of user.
     * @return
     */
    public ServerConnectionThread getServerConnectionThread(String gameId,
            long username) {
        if (gameId != null) {
            if (gameId.equals(IGame.ADMINISTRATOR_GAME)) {
                return getConnection(IGame.ADMINISTRATOR_GAME,
                                     IGame.ADMINISTRATOR).
                        getServerConnectionThread();
            } else {
                return getConnection(gameId, username).
                        getServerConnectionThread();
            }
        }

        return null;
    }

    /**
     * Return a Connection object of specified user.
     *
     * @param username
     * @return
     */
    public Connection getConnection(String gameId, long username) {
        return (Connection) connections.get(getKey(gameId, username));
    }

    /**
     * Remove a connection.
     *
     * @param username
     */
    public void removeConnection(String gameId, long username) {
        connections.remove(getKey(gameId, username));
    }

    /**
     * Remove the admin connection.
     */
    public void removeAdminConnection() {
        connections.remove(getKey(IGame.ADMINISTRATOR_GAME, IGame.ADMINISTRATOR));
    }

    /**
     * Return number of connections.
     *
     * @return
     */
    public int size() {
        return connections.size();
    }

    /**
     * Return hash key from a gameId and a username.
     *
     * @param gameId   GameID
     * @param username Username
     * @return
     */
    private String getKey(String gameId, long username) {
        return gameId + "-" + username;
    }

    /**
     * Return admin connection.
     *
     * @return
     */
    public ServerConnectionThread getAdminConnection() {
        Connection conn = getConnection(IGame.ADMINISTRATOR_GAME,
                                        IGame.ADMINISTRATOR);

        if (conn != null) {
            return conn.getServerConnectionThread();
        }

        return null;
    }
}
